import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/cooperation_controller/my_invitation_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/utils/refresh_util.dart';
import 'package:xinxuan/vo/user_invitations_vo.dart';

class MyInvitationPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: pageBgColor,
      appBar: AppBarUtil(
        title: '我邀请的',
        backgroundColor: bgWhiteColor,
      ),
      body: GetBuilder<MyInvitationPageController>(
        init: MyInvitationPageController(),
        builder: (controller) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _typeBar(controller),
              Expanded(child: RefreshUtil.easyRefresh(
                onRefresh: () async {
                  controller.getUserInvitations();
                },
                onLoad: () async {
                  controller.onLoadUserInvitations();
                },
                slivers: [
                  SliverList(
                    delegate: SliverChildBuilderDelegate((context, index) {
                      UserInvitationsItem item = controller.userInvitationsVo.value.data![index];
                      return Container(
                        color: bgWhiteColor,
                        padding: EdgeInsets.symmetric(horizontal: 12.w),
                        child: Container(
                          padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 16.h),
                          decoration: BoxDecoration(
                            border: Border(
                              bottom: BorderSide(
                                color: lineColor,
                                width: .5
                              )
                            )
                          ),
                          child: Row(
                            children: [
                              NetworkImageUtil(
                                url: '',
                                nickname: '${item.username}', 
                                width: 36.w, 
                                height: 36.w,
                                showShadow: false,
                              ),
                              SizedBox(width: 12.w),
                              Expanded(child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    '${item.username}',
                                    style: TextStyle(
                                      color: textBlackColor,
                                      fontSize: 16.sp,
                                      fontWeight: FontWeight.bold
                                    ),
                                  ),
                                  Text(
                                    '${item.createdAt}',
                                    style: TextStyle(
                                      color: textSubtitleColor,
                                      fontSize: 12.sp
                                    ),
                                  )
                                ],
                              )),
                              Text(
                                item.isPurchased == 1 ? '已完成' : '未完成',
                                style: TextStyle(
                                  color: item.isPurchased == 1 ? textSuccessColor : mainTextColor,
                                  fontSize: 16.sp
                                ),
                              )
                            ],
                          ),
                        ),
                      );
                    }, childCount: controller.userInvitationsVo.value.data!.length),
                  )
                ]
              ))
            ],
          );
        },
      ),
    );
  }

  /// 类型
  Widget _typeBar(MyInvitationPageController controller) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
      child: InkWellUtil.shakeProofInkWell(Container(
        height: 36.h,
        width: 90.w,
        decoration: BoxDecoration(
          color: lineColor,
          borderRadius: BorderRadius.circular(36.h)
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Obx(() => Text(
              '${controller.typeStr.value}',
              style: TextStyle(
                color: textBlackColor,
                fontSize: 14.sp
              ),
            )),
            SizedBox(width: 4.w),
            Image.asset(
              'assets/icons/icon_arrow_down_b.png',
              width: 6.w,
              height: 6.w,
            )
          ],
        ),
      ), () {
        Get.bottomSheet(Container(
          height: 300.h,
          decoration: BoxDecoration(
            color: bgWhiteColor,
            borderRadius: BorderRadius.vertical(top: Radius.circular(5.w))
          ),
          child: Column(
            children: [
              Container(
                height: 54.h,
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    InkWellUtil.shakeProofInkWell(Text(
                      '取消',
                      style: TextStyle(
                        color: textSubtitleColor,
                        fontSize: 16.sp
                      ),
                    ), () {
                      Get.back();
                    }),
                    InkWellUtil.shakeProofInkWell(Text(
                      '确认',
                      style: TextStyle(
                        color: mainTextColor,
                        fontSize: 16.sp
                      ),
                    ), () {
                      controller.determineType();
                      Get.back();
                    })
                  ],
                ),
              ),
              Expanded(
                child: ScrollConfigurationCommon(child: CupertinoPicker(
                  scrollController: controller.typeScrollController.value,
                  diameterRatio: 5.w,
                  itemExtent: 54.h, 
                  onSelectedItemChanged: (int index) {
                    controller.changeTypeIndex(index);
                  }, 
                  children: controller.typeArr.map((item) {
                    return Container(
                      height: 54.h,
                      alignment: Alignment.center,
                      child: Text(
                        '${item['typeStr']}',
                        style: TextStyle(
                          color: textBlackColor,
                          fontSize: 16.sp
                        ),
                      ),
                    );
                  }).toList(),
                )),
              )
            ],
          ),
        ));
      })
    );
  }

}